package com.javalong.affairs.module.renwujinzhang.activity.receive;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.EditText;

import com.google.gson.reflect.TypeToken;
import com.javalong.affairs.R;
import com.javalong.affairs.base.common.ISynCallback;
import com.javalong.affairs.base.common.UIStatus;
import com.javalong.affairs.base.common.ViewHolder;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.RequestCode;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.IntUtils;
import com.javalong.affairs.base.utils.StringUtils;
import com.javalong.affairs.base.utils.TimeUtils;
import com.javalong.affairs.base.utils.ToastUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.javalong.affairs.module.notifything.activity.NewReceiverActivity;
import com.javalong.affairs.module.renwujinzhang.activity.AuditoerActivity;
import com.javalong.affairs.module.renwujinzhang.activity.NewNodeActivity;
import com.javalong.affairs.module.renwujinzhang.activity.RecycleListActivity;
import com.javalong.affairs.module.renwujinzhang.activity.SupervisesActivity;
import com.javalong.affairs.module.renwujinzhang.activity.progress.ProgressActivity;
import com.javalong.affairs.module.renwujinzhang.activity.submit.TaskSubmitActivity;
import com.javalong.affairs.module.renwujinzhang.activity.suggest.activity.SuggestListActivity;
import com.javalong.affairs.module.renwujinzhang.model.ProgressModel;
import com.javalong.affairs.module.renwujinzhang.model.Task;
import com.javalong.affairs.module.renwujinzhang.ui.SelectTimeView;
import com.javalong.affairs.module.renwujinzhang.ui.SelectUserView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 接收任务中 的已提交的任务
 */
public class WatchCommitTaskActivity extends RecycleListActivity implements ISynCallback {

    private Task task;

    public static void toThisActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, WatchCommitTaskActivity.class);
        context.startActivity(intent);
    }


    public static void toThisActivity(Context context, Task task) {
        Intent intent = new Intent();
        intent.setClass(context, WatchCommitTaskActivity.class);
        intent.putExtra(BundleKey.TASK, task);
        context.startActivity(intent);
    }


    @Override
    public void afterInit() {
        getSupportActionBar().setTitle("任务详情");
        task = (Task) getIntent().getSerializableExtra(BundleKey.TASK);
        uiHandler.registerCallback(this, flag);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TIME:
                return new ViewHolder(new SelectTimeView(this));
            case MULTI_USER:
                return new ViewHolder(new SelectUserView(this));
            case EDIT:
                return new ViewHolder(mInflater.inflate(R.layout.item_imageedit, parent, false));
            case BUTTON:
                return new ViewHolder(mInflater.inflate(R.layout.item_button, parent, false));
        }
        return new ViewHolder(mInflater.inflate(R.layout.item_imageedit, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        switch (position) {
            case 0: {
                EditText editText = holder.getView(R.id.et_right);
                editText.setEnabled(false);
                editText.setHint(task.getTitle());
            }
            break;
            case 1: {
                EditText editText = holder.getView(R.id.et_right);
                editText.setEnabled(false);
                editText.setHint(task.getContent());
            }
            break;
            case 2: {
                holder.itemView.setEnabled(false);
                ((SelectTimeView) holder.itemView).setHintText(TimeUtils.getFormatTime2(task.getTimeBegin()));
                ((SelectTimeView) holder.itemView).setStatus(UIStatus.WATCH);
            }
            break;
            case 3: {
                holder.itemView.setEnabled(false);
                ((SelectTimeView) holder.itemView).setHintText(TimeUtils.getFormatTime2(task.getTimeEnd()));
                ((SelectTimeView) holder.itemView).setStatus(UIStatus.WATCH);
            }
            break;
            case 4: {
                holder.itemView.setEnabled(false);
                ((SelectUserView) holder.itemView).setHintText(task.getReceiveName());
                ((SelectUserView) holder.itemView).setStatus(UIStatus.WATCH);
            }
            break;
        }
    }

    private void watchTask() {
        String url = UrlUtils.getUrl("Task", "outList_detail_splitList");
        Map<String, Object> param = new HashMap<String, Object>();
        param.put(RequestKey.ID, task.getId());
        HttpUtils.post(url, param, new BaseResponseListener(
                new TypeToken<List<List<ProgressModel>>>() {
                }.getType()) {
            @Override
            public void onSuccess(Object obj) {
                ProgressActivity.toThisActivity(WatchCommitTaskActivity.this, (List<List<ProgressModel>>) obj);
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return EDIT;
            case 1:
                return EDIT;
            case 2:
                return TIME;
            case 3:
                return TIME;
            case 4:
                return MULTI_USER;
        }
        return 0;
    }

    @Override
    public int getItemCount() {
        return 5;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_task_receive_commit, menu);
        return true;
    }

    private int flag = IntUtils.getNextInt();

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_supervise:
                SupervisesActivity.toThisActivity(WatchCommitTaskActivity.this, task.getId());
                break;
            case R.id.action_audit:
                AuditoerActivity.toThisActivity(WatchCommitTaskActivity.this, task.getId());
                break;
            case R.id.action_watch_progress:
                watchTask();
                break;
            case R.id.action_suggest:
                SuggestListActivity.toThisActivity(WatchCommitTaskActivity.this, task.getId());
                break;
            case R.id.action_work_report: {
                Bundle bundle = new Bundle();
                bundle.putInt("type", 0);
                bundle.putInt("flag", flag);
                IntentManager.doActivityForResult(this,
                        NewReceiverActivity.class, RequestCode.RECEIVER, bundle);
            }
            break;
            case R.id.action_cooperation: {
                Bundle bundle = new Bundle();
                bundle.putInt("type", 0);
                bundle.putInt("flag", flag);
                IntentManager.doActivityForResult(this,
                        NewReceiverActivity.class, RequestCode.RECEIVER, bundle);
            }
            break;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        uiHandler.unRegisterCallback(this, flag);
    }

    @Override
    public void OnSynCalled(int ulID, Bundle bundle) {
        if (flag == ulID) {
            List<User> userList = (List<User>) bundle.getSerializable(BundleKey.USERLIST);
            String url = UrlUtils.getUrl("Task", "action_superviseAdd");
            Map<String, Object> params = new HashMap<String, Object>();
            params.put(RequestKey.ID, task.getId());
            params.put(RequestKey.SUPERVISES, StringUtils.getUserIds(userList));
            HttpUtils.post(url, params, new BaseResponseListener(null) {
                @Override
                public void onSuccess(Object obj) {
                    super.onSuccess(obj);
                }
            });
        }
    }
}
